package com.yangtao.prototype.master.dao;

import com.yangtao.framework.common.Option;
import com.yangtao.framework.common.SystemConstant;
import com.yangtao.framework.hibernate.BaseDao;
import com.yangtao.framework.mapping.Mapper;
import com.yangtao.prototype.master.domain.Constant;
import org.apache.commons.lang.xwork.StringUtils;
import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class ConstantDao extends BaseDao<Constant> {

	@Autowired
	private Mapper mapper;

	/**
	 * 根据类型查询下拉单内容
	 *
	 * @param types  类型
	 * @param userId 用户ID
	 * @return 下拉单内容
	 */
	public HashMap<String, List<Option>> getOptionList(String[] types,
	                                                   String userId) {
		Assert.notEmpty(types, "类型不能为空");
		HashMap<String, List<Option>> ret = new HashMap<String, List<Option>>();
		StringBuilder sql = new StringBuilder(
				"select new com.yangtao.framework.common.Option(c.code,c.name) from Constant c where c.type=? and c.isDelete=?");
		if (StringUtils.isNotBlank(userId)) {
			sql.append(" and c.userId = '");
			sql.append(userId.trim());
			sql.append("'");
		}
		for (int i = 0; i < types.length; i++) {
			List<Option> lst = find(sql.toString(), types[i].trim(),
					SystemConstant.LOGIC_FALSE);
			ret.put(types[i], lst);
		}
		return ret;
	}

	/**
	 * 根据类型查询下拉列表
	 *
	 * @param type 类型
	 * @return
	 */
	public List<Option> findByType(String type) {
		String hql = "select new com.yangtao.framework.common.Option(c.code,c.name) from Constant c where c.type=?";
		Query query = getSession().createQuery(hql);
		query.setString(0, type);
		query.setCacheable(true);
		return query.list();
	}

	/**
	 * 根据类型查询下拉单
	 *
	 * @param companyId 公司Id
	 * @param type      类型
	 * @return
	 */
	public List<Option> findByType(String companyId, String type) {
		String hql = "select new com.yangtao.framework.common.Option(c.code,c.name) from Constant c where c.type=?";
		//TODO 增加公司的筛选条件
		return find(hql, companyId, type);
	}

	public List<Option> getAllList(String companyId) {
		StringBuilder sql = new StringBuilder(
				"select distinct c.type,c.typeName from Constant c where c.isSystem=:isSystem");
		Map<String, Object> paraterMap = new HashMap<String, Object>();
		paraterMap.put("isSystem", SystemConstant.LOGIC_FALSE);
		// if (StringHelper.isNotEmpty(companyId)) {
		// sql.append(" and c.companyId=:companyId");
		// paraterMap.put("companyId", companyId);
		// }
		List<Object[]> objs = find(sql.toString(), paraterMap);
		List<Option> ret = new ArrayList<Option>();
		for (Object[] obj : objs) {
			ret.add(new Option(obj[0].toString(), obj[1].toString()));
		}
		return ret;
	}

	public List<Constant> findByCode(String code) {
		return find("from Constant c where c.code like ?", code + "%");
	}

	/**
	 * 根据code和type取得常量的值
	 *
	 * @param type 常量类型
	 * @param code 常量代码
	 * @return
	 */
	public String findValue(String type, String code) {
		String hql = "select c.name from Constant c where c.type=:type and c.code = :code";
		Query query = createQuery(hql, type, code);
		query.setCacheable(true);
		return (String) query.uniqueResult();
	}

	/**
	 * 修改一个list内的所有记录
	 *
	 * @param list
	 */
	public void updateAll(List<Constant> list) {
		for (Constant c : list) {
			// 先加载出对象
			Constant ct = load(c.getId());
			if (ct != null) {
				// copy从页面出过来的属性
				mapper.map(c, ct);
			}
		}
	}
}
